//
//  YLWKWebViewModel.m
//  yuLink
//
//  Created by aiwoba on 2021/4/13.
//

#import "YLWKWebViewModel.h"
#import "SAMKeychain.h"
#import "NetworkManager.h"

@implementation YLWKWebViewModel

+ (NSDictionary *)getWebViewDataByUrlString:(NSString *)urlString {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    
    NSString *actionName = nil;
    NSDictionary *paramsDic = nil;
    NSString *callbackStr = nil;
    NSRange range = [urlString rangeOfString:@"?"];
    if (range.length == 0) {
        actionName = urlString;
    }else {
        actionName = [urlString substringToIndex:range.location];
        NSString *otherStr = [urlString substringFromIndex:range.location + range.length];
        NSArray *tempArray = [otherStr componentsSeparatedByString:@"&"];
        for (NSString *obj in tempArray) {
            if ([obj containsString:@"params="]) {
                NSRange range = [obj rangeOfString:@"params="];
                if (range.length > 0) {
                    NSString * tempStr= [obj substringFromIndex:range.location + range.length];
                    NSData *jsonData = [[YLAppUtils URLDecode:tempStr] dataUsingEncoding:NSUTF8StringEncoding];
                    NSError *serializationError = nil;
                    paramsDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&serializationError];
                }
            } else if ([obj containsString:@"callback="]) {
                NSRange range = [obj rangeOfString:@"callback="];
                if (range.length > 0) {
                    callbackStr = [obj substringFromIndex:range.location + range.length];
                }
            }
        }
    }
    
    if ([YLAppUtils judgeStrIsEmpty:actionName]) {
        actionName = @"";
    }
    if (!paramsDic) {
        paramsDic = @{};
    }
    if ([YLAppUtils judgeStrIsEmpty:callbackStr]) {
        callbackStr = @"";
    }
    
    [mutableDict setObject:actionName forKey:@"actionName"];
    [mutableDict setObject:paramsDic forKey:@"params"];
    [mutableDict setObject:callbackStr forKey:@"callback"];
    
    return mutableDict;
}

+ (NSString *)defaultCookieConfiguration {
    
    NSMutableString *tempCookieStr = [NSMutableString string];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:11];
//    [dic setObject:[YLAppUtils filterNull:[REAccountTool account].userNo] forKey:@"user_id"];
    [dic setObject:[YLAppUtils filterNull:GetToken] forKey:@"access_token"];
    [dic setObject:[YLAppUtils getUUID] forKey:@"client_token"];
    [dic setObject:[YLAppUtils getAppVersion] forKey:@"client_version"];
    [dic setObject:@"app_mobile" forKey:@"login_type"];//app_mobile（app中手机号）,app_wechat（app中微信）
    [dic setObject:@"1" forKey:@"method_version"];
    [dic setObject:[NetworkManager getCurrentNetwork] forKey:@"network_type"];
    [dic setObject:@"ios" forKey:@"platform"];
    [dic setObject:[YLAppUtils getIOSVersion] forKey:@"system_version"];
    [dic setObject:[NSNumber numberWithBool:[YLAppUtils isIphonex]] forKey:@"isIphonex"];
    [dic setObject:@"ymc" forKey:@"x-app-name"];

     NSArray *allKeys = [dic allKeys];
    for (NSString *key in allKeys) {
        [tempCookieStr appendString:[NSString stringWithFormat:@"document.cookie = '%@=%@;path=/';",key,dic[key]]];
    }
    
    return tempCookieStr;
}

@end
